package com.study.userserviceprovider.costomPropertySource;

import org.springframework.boot.env.OriginTrackedMapPropertySource;
import org.springframework.boot.env.PropertySourceLoader;
import org.springframework.boot.json.JsonParser;
import org.springframework.boot.json.JsonParserFactory;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description：对文件类型解析到spring环境中的扩展
 * @author: 李军容
 * @version: 2020/12/2 20:49
 */
public class CustomJsonPropertiesLoader implements PropertySourceLoader{

    @Override
    public String[] getFileExtensions() {
        return new String[]{"json"};
    }

    @Override
    public List<PropertySource<?>> load(String name, Resource resource) throws IOException {
        Map<String, ?> properties = loadProperties(resource);
        if (properties.isEmpty()) {
            return Collections.emptyList();
        }
        return Collections
                .singletonList(new OriginTrackedMapPropertySource(name, Collections.unmodifiableMap(properties), true));
    }


    private Map<String, Object> loadProperties(Resource resource) {
        String fileContext = getFileContext(resource);
        if(null == fileContext){
            return  null;
        }
        JsonParser jsonParser = JsonParserFactory.getJsonParser();
        Map<String,Object>  fileMap = jsonParser.parseMap(fileContext);
        Map<String,Object> enviromentMap = new HashMap<String,Object>();
        parseJsonMap("",fileMap,enviromentMap);
        return enviromentMap;
    }

    private void parseJsonMap(String prefix, Map<String, Object> fileMap,
                              Map<String, Object> result) {
        if(prefix.length() > 0){
            prefix += ".";
        }

        for(String key : fileMap.keySet()){
            Object value = fileMap.get(key);
            prefix += key;
            if(value instanceof Map){
                parseJsonMap(prefix, (Map<String, Object>) value,result);
            }else{
                result.put(prefix,value);
            }
        }
    }

    private String getFileContext( Resource resource) {
        if(null == resource){
            return  null;
        }
        return loadFileIo(resource);
    }

    private String loadFileIo(Resource resource) {
        try(FileInputStream fis  = new FileInputStream(resource.getFile());
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);){
            byte[] b = new byte[1024];
            int len = -1;
            while((len = fis.read(b)) != -1) {
                bos.write(b, 0, len);
            }
            byte[] buffer = bos.toByteArray();
            return new String(buffer, "UTF-8");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
